package cn.darkhorse.mesadmin.controller.device;


import cn.darkhorse.common.util.Base64Util;
import cn.darkhorse.device.pojo.MesCamera;
import cn.darkhorse.device.service.MesCameraService;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 摄像机设置 前端控制器
 * </p>
 *
 * @author maoguanjun
 * @since 2023-04-20
 */
@RestController
@RequestMapping("/device/mesCamera")
public class MesCameraController extends BaseController {
    @Resource
    private MesCameraService mesCameraService;

    /**
     * 带分页
     * @param name
     * @param area
     * @param ip
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public TableDataInfo list(String name, String area, String ip){
        startPage();
        List<MesCamera> list=mesCameraService.listAll(name,area,ip);
        return getDataTable(list);
    }

    /**
     * 不带分页
     * @param name
     * @param area
     * @param ip
     * @return
     */
    @RequestMapping(value = "/list-all",method = RequestMethod.POST)
    public AjaxResult listAll(String name, String area, String ip){
        List<MesCamera> list=mesCameraService.listAll(name,area,ip);
        return AjaxResult.success(list);
    }

    @RequestMapping(value = "/view",method = RequestMethod.POST)
    public AjaxResult view(Long cameraId){
        MesCamera mesCamera=mesCameraService.getById(cameraId);
        return AjaxResult.success(mesCamera);
    }
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public AjaxResult save(@RequestBody MesCamera mesCamera){
        if(StringUtils.isBlank(mesCamera.getName())){
            return AjaxResult.error("名称不能为空!");
        }
        if(StringUtils.isBlank(mesCamera.getArea())){
            return AjaxResult.error("所属区域不能为空!");
        }
        boolean b=mesCameraService.saveEntity(mesCamera);
        return AjaxResult.success(b);
    }
    @RequestMapping(value = "/getCode",method = RequestMethod.POST)
    public AjaxResult save(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code","camera"+System.currentTimeMillis());
        return AjaxResult.success(jsonObject);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public AjaxResult delete(Long cameraId){
        if(null==cameraId){
            AjaxResult.error("缺失参数！");
        }
        boolean b=mesCameraService.delete(cameraId);
        return AjaxResult.success(b);
    }

    /**
     * 解码
     * @param key
     * @return
     */
    @RequestMapping(value = "/decry",method = RequestMethod.POST)
    public AjaxResult decry(String key){
        String pwd="";
        byte[] bytes = new byte[0];
        try {
            bytes = Base64Util.decryBASE64(key);
            pwd=new String(bytes, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return AjaxResult.success(pwd);
    }
}

